package logic

import (
	"github.com/gin-gonic/gin"
	"iot-base/auth/config"
	"iot-base/auth/model"
	"iot-base/auth/svc"
	types2 "iot-base/auth/types"
	"sort"
	"strings"
	"time"
)

type RulesLogic struct {
}

func NewRulesLogic() *RulesLogic {
	return &RulesLogic{}
}

func (l *RulesLogic) AddRule(reqRule types2.ReqAuthRule) error {
	var rule model.AuthRule
	rule.Title = reqRule.Title
	rule.Type = reqRule.Type
	rule.AuthRule = reqRule.AuthRule
	rule.Weigh = reqRule.Weigh
	rule.Remark = reqRule.Remark
	rule.Extend = reqRule.Extend
	rule.Pid = reqRule.Pid
	rule.AuthCode = reqRule.AuthCode
	rule.Path = reqRule.Path
	rule.Icon = reqRule.Icon
	rule.RouterName = reqRule.RouterName
	rule.Method = reqRule.Method
	rule.RouterPath = reqRule.RouterPath
	rule.KeepAlive = reqRule.KeepAlive
	rule.CreatedAt = time.Now()
	rule.UpdatedAt = time.Now()
	return svc.Dao.TAuthRule.AddRule(&rule)
}

func (l *RulesLogic) DelRule(id int64) error {
	return svc.Dao.TAuthRule.DelRule(id)
}

func (l *RulesLogic) EditRule(reqRule types2.ReqAuthRule) error {
	var rule model.AuthRule
	rule.ID = reqRule.Id
	rule.Title = reqRule.Title
	rule.Type = reqRule.Type
	rule.AuthRule = reqRule.AuthRule
	rule.Weigh = reqRule.Weigh
	rule.Remark = reqRule.Remark
	rule.Extend = reqRule.Extend
	rule.Pid = reqRule.Pid
	rule.AuthCode = reqRule.AuthCode
	rule.Path = reqRule.Path
	rule.Icon = reqRule.Icon
	rule.RouterName = reqRule.RouterName
	rule.Method = reqRule.Method
	rule.RouterPath = reqRule.RouterPath
	rule.KeepAlive = reqRule.KeepAlive
	rule.UpdatedAt = time.Now()
	return svc.Dao.TAuthRule.EditRule(rule)
}

func (l *RulesLogic) FindRuleAll() ([]model.AuthRule, error) {
	return svc.Dao.TAuthRule.FindRuleAll()
}

func (l *RulesLogic) FindRule(page, size, ruleType int) ([]model.AuthRule, error) {
	return svc.Dao.TAuthRule.FindRule(page, size, ruleType)
}

func (l *RulesLogic) FindRuleCount(ruleType int) (int64, error) {
	return svc.Dao.TAuthRule.FindRuleCount(ruleType)
}

func (l *RulesLogic) FindUserRule(uid int64) ([]model.AuthRule, error) {
	var resp []model.AuthRule
	//ruleModel := model.NewAuthRuleModel()
	return resp, nil
}

// FindMenu 查询菜单.
func (l *RulesLogic) FindMenu(uid int64, groupId int64, ruleType int) ([]model.AuthRule, error) {
	resp := make([]model.AuthRule, 0, 0)
	authRole, err := svc.Dao.TAuthRole.GetRole(uid, groupId)
	if err != nil {
		return resp, err
	}
	ruleIds := ""
	var isAdmin bool
	for _, v := range authRole {
		if v.Rules == "*" {
			isAdmin = true
			break
		} else {
			ruleIds += "," + v.Rules
		}
	}
	if isAdmin {
		resp, err = svc.Dao.TAuthRule.FindRuleAll()
		if err != nil {
			return resp, err
		}
		return resp, nil
	} else {
		if ruleIds == "" {
			return resp, err
		}
		resp, err = svc.Dao.TAuthRule.FindRuleByIds(strings.Split(strings.Trim(ruleIds, ","), ","), ruleType)
		if err != nil {
			return nil, err
		}
		return resp, nil
	}

}
func (l *RulesLogic) FindGroupMenu(uid int64) ([]interface{}, error) {
	resp := make([]interface{}, 0)
	groupList, err := svc.Dao.TAuthGroup.GetUserGroup(uid)
	if err != nil {
		return resp, err
	}
	sort.Slice(groupList, func(i, j int) bool {
		return groupList[i].ID < groupList[j].ID
	})
	for _, v := range groupList {
		rules, err := l.FindMenu(uid, v.ID, 0)
		if err != nil {
			continue
		}
		data := gin.H{
			"group_id":   v.ID,
			"group_name": v.GroupName,
			"rules":      rules,
		}
		resp = append(resp, data)
	}
	return resp, err
}
func (l *RulesLogic) FindMenuRule(uid int64) (map[string]string, error) {
	resp := make(map[string]string)
	result, err := l.FindMenu(uid, 0, 2)
	if err != nil {
		return resp, err
	}
	for _, v := range result {
		rule := "/" + config.Conf.Server.Version + v.AuthRule + ":" + v.Method
		resp[rule] = "1"
	}
	return resp, nil
}
